
var backendWebUrl = "/gateway/ERP-BACKEND-SERVICE-DEV";
var schoolWebUrl = "/gateway/ERP-SCHOOL-SERVICE-DEV";
var messageWebUrl = "/gateway/ERP-MESSAGE-SERVICE-DEV";
var fileServiceUrl = "/gateway/ERP-FILE-SERVICE-DEV";
var govservicUrl="/gateway/ERP-GOV-SERVICE-DEV";

var systemhl="/erp/hl"

//文件上传和显示地址
var fileUploadUrl = fileServiceUrl + "/file/upload";
var fileShowUrl = fileServiceUrl + "/file/show";

//首页文件上传和显示地址(忽略token)
var indexUploadUrl = fileServiceUrl + "erp/hl/file/upload";
var indexShowUrl = fileServiceUrl + "erp/hl/file/show";

//文件上传参数
var appId="1";
var type=1;


var SystemId = "school-web";
var gatewayAuthorization = SystemId + "-Authorization";
var currentUserId = SystemId + "-currentUserId";
var currentUserName = SystemId + "-currentUserName";
var currentMenuList = SystemId + "-currentMenuList";


// 顶级Iframe
var topFrame = top.frames["pageiframe"];

window.console = window.console || (function () {
    var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile
        = c.clear = c.exception = c.trace = c.assert = function () { };
    return c;
})();


function loginFilter() {
    if (!$.cookie(gatewayAuthorization)) {
        var tp = window;
        while ((!tp.autoForwardLoginPage) && tp.parent != tp) {
            tp = tp.parent
        }
        if(tp.autoForwardLoginPage){
            tp.autoForwardLoginPage();
        }
    }
}

//正则表达式，获取地址中的参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires+"; path=/";//path=/是根路径
}
//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}
//清除cookie
function clearCookie(name) {
    setCookie(name, "", -1);
}

function getRootPath() {
    var scripts = document.getElementsByTagName("script")
    var script = scripts[scripts.length - 1];
    strJsPath = document.querySelector ? script.src : script.getAttribute("src")//IE8直接.src
    return strJsPath.substr(0, strJsPath.lastIndexOf("/static"));
}


var sysRootPath = getRootPath();


function dynamicLoadJs(url, callback) {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');

    script.type = 'text/javascript';
    script.src = url;
    if (typeof(callback) == 'function') {
        script.onload = script.onreadystatechange = function () {
            if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
                callback();
                script.onload = script.onreadystatechange = null;
            }
        };
    }
    head.appendChild(script);
}


function GetHttpRequest() {
    if (window.XMLHttpRequest) // Gecko
        return new XMLHttpRequest();
    else if (window.ActiveXObject) // IE
        return new ActiveXObject("MsXml2.XmlHttp");
}


function AjaxPage(sId, url) {
    var oXmlHttp = GetHttpRequest();
    oXmlHttp.onload = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200 || oXmlHttp.status == 304) {
                IncludeJS(sId, url, oXmlHttp.responseText);
            }
            else {
                alert('XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')');
            }
        }
    }
    oXmlHttp.open('GET', url, false);
    oXmlHttp.send(null);
}


function IncludeJS(sId, fileUrl, source) {
    if ((source != null) && (!document.getElementById(sId))) {
        var oHead = document.getElementsByTagName('HEAD').item(0);
        var oScript = document.createElement("script");
        oScript.language = "javascript";
        oScript.type = "text/javascript";
        oScript.id = sId;
        oScript.defer = true;
        oScript.text = source;
        oHead.appendChild(oScript);
    }
}

function loadBaseJS() {
    AjaxPage("jquery.min", sysRootPath + "/static/js/jquery.min.js");
    AjaxPage("jquery.cookie", sysRootPath + "/static/js/jquery.cookie.js");
    AjaxPage("ax", sysRootPath + "/static/js/ax.js");
}
